<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="javabean.*"%>
<%@page import="javabean.book.*"%>
<!DOCTYPE html>
<html lang="zh-cn">
	<%
		String uid = (String)session.getAttribute("uid");
		boolean isRole = false;
		boolean isSendDebit = false;
		if(uid!=null){
			isRole = (Boolean)request.getAttribute("isRole");
			isSendDebit = (Boolean)request.getAttribute("isSendDebit");
		}
		//图书详细信息
		List all = (List<Book>)request.getAttribute("bookInfo");
		Book book = (Book)all.get(0);
		long  bid = book.getBid() ;
		String isbn = book.getIsbn() ;
		String bname = book.getBname() ;
		String author = book.getAuthor() ;
		String publisher = book.getPublisher();
		String years = book.getYears();
		String revision = book.getRevision();
		String belong = book.getBelong();
		String debit = book.getDebit()==null?"":book.getDebit();
		String exterior = book.getExterior();
		String picurl = book.getPicurl();
		String introduce = book.getOther();
		String type = book.getType();
		int quantity = book.getQuantity();
		int ischeck = book.getIsCheck();
		//图书评价（评分信息）
		List bcAll = (List<BookComment>)request.getAttribute("bookComment");
		Iterator<BookComment> bcAllIter = bcAll.iterator() ;
		double avg = 0.0;
		String bigstarClass = "";
		long bcid = 0; //评论(评分)的唯一标识
		int count = 0;	//评分总人数
		double twoCount = 0.0;	//评分为2的人数
		double foreCount = 0.0;
		double sixCount = 0.0;
		double eightCount = 0.0;
		double tentCount = 0.0;
		int myScore = 0 ; //我的评分
		ParseDouble parse = new ParseDouble();	//转换类
		while(bcAllIter.hasNext()){
			BookComment bookComment = (BookComment)bcAllIter.next();
			int score = bookComment.getScore();
			if(score!=0){
				avg += score;
				if(score==2){
					twoCount += 1;
				}else if(score==4){
					foreCount += 1;
				}else if(score==6){
					sixCount += 1;
				}else if(score==8){
					eightCount += 1;
				}else if(score==10){
					tentCount += 1;
				}
				//我的评分
				if(uid!=null && uid.equals(bookComment.getUid()) && bookComment.getScore()!=0){
					myScore = bookComment.getScore();
					bcid = bookComment.getBcid();
				}
				count++;
			}
		}
		avg = parse.parseOnePoint(avg/count);	//转换成一个小数点
		//判断平均分，匹配对应星星
		if(avg==0.0){
			bigstarClass = "bigstar00";
		}else if(avg>0 && avg<=1.5){
			bigstarClass = "bigstar05";
		}else if(avg>1.5 && avg<=2.5){
			bigstarClass = "bigstar10";
		}else if(avg>2.5 && avg<=3.5){
			bigstarClass = "bigstar15";
		}else if(avg>3.5 && avg<=4.5){
			bigstarClass = "bigstar20";
		}else if(avg>4.5 && avg<=5.5){
			bigstarClass = "bigstar25";
		}else if(avg>5.5 && avg<=6.5){
			bigstarClass = "bigstar30";
		}else if(avg>6.5 && avg<=7.5){
			bigstarClass = "bigstar35";
		}else if(avg>7.5 && avg<=8.5){
			bigstarClass = "bigstar40";
		}else if(avg>8.5 && avg<=9.5){
			bigstarClass = "bigstar45";
		}else if(avg>9.5 && avg<=10.0){
			bigstarClass = "bigstar50";
		}
		//百分比转换
		twoCount = parse.parsePercent(twoCount/count);
		foreCount = parse.parsePercent(foreCount/count);
		sixCount = parse.parsePercent(sixCount/count);
		eightCount = parse.parsePercent(eightCount/count);
		tentCount = parse.parsePercent(tentCount/count);
		//判断是否有评分，并定义相应tips和是否显示删除
		String tip ="",delDisplay="";
		String[] tips = {"垃圾","没啥感觉","还过得去","挺不错的","太好看啦"};
		if(myScore!=0){
			tip = tips[myScore/2-1];
			delDisplay="inline";
		}

	%>
    <head>
    	<title><%=book.getBname() %></title>
    	<meta charset="utf-8">
    	<meta http-equiv="Cache-Control" CONTENT="no-cache">
        <link rel="stylesheet" type="text/css" href="/bookcrossing/css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="/bookcrossing/css/main.css">
        <link rel="stylesheet" type="text/css" href="/bookcrossing/css/book.css">
        <link rel="shortcut icon" type="image/png" href="/bookcrossing/img/favicon.png" />
	</head>
	<body>
		<jsp:include page="../public/head.jsp"></jsp:include>
		<div class="container b-i-c-wrap">
			<div class="book-info-container">
				<div class="modal fade" id="book-info-dialog-box">
			    	<div class="modal-dialog">
			    		<div class="modal-content">
			    			<div class="modal-header">
			    				<p class="title">借书申请</p>
			    			</div>
			    			<form class="form-horizontal" id="send-tips-form">
				    			<div class="modal-body">
				    				<div class="form-group">
				    					<label for="receiver" class="col-sm-2 control-label">发给:</label>
									    <div class="col-sm-9">
									      <input type="text" class="form-control receiver input-sm" id="receiver" name="receiver" value="<%=belong %>" readonly >
									    </div>
				    				</div>
				    				<div class="form-group">
				    					<label for="tcontents" class="col-sm-2 control-label">内容:</label>
									    <div class="col-sm-9">
									      <textarea class="form-control tcontents input-sm" id="tcontents" name="tcontents" placeholder="还可以说点什么哦~" rows="4" ></textarea>
									    </div>
				    				</div>
				    			</div>
				    			<div class="modal-footer">
				    				<button type="submit" class="btn btn-info btn-sm submit-btn">发送</button>
				    				<button type="button" class="btn btn-warning btn-sm cancel-btn" data-dismiss="modal">取消</button>
				    			</div>
			    			</form>
			    		</div>
			    	</div>
			    </div>
				<a href="javascript:history.go(-1);" class="go-back"><< 返回上一页</a>
				<h3 class="b-i-title">图书详情</h3>
				<div class="b-i-content">
					<div class="b-i-left">
						<div class="b-i-pic">
							<img src="/bookcrossing/img/book/<%=picurl %>" />
						</div>
						<div class="b-i-info">
							<ul>
								<li>
									<span class="bname"><%=bname %></span>
								</li>
								<li>
									<span class="">作者: </span><span class="author"><%=author %></span>
								</li>
								<%if(publisher !=null && !"".equals(publisher)){%>
								<li>
									<span class="">出版社: </span><span class="publisher"><%=publisher %></span>
								</li>
								<%} %>
								<%if(years !=null && !"".equals(years)){%>
									<li>
										<span class="">出版年: </span><span class="years"><%=years %></span>
									</li>
								<%} %>
								<%if(revision !=null && !"".equals(revision)){%>
									<li>
										<span class="">版次: </span><span class="revision"><%=revision %></span>
									</li>
								<%} %>
								<%if(exterior !=null && !"".equals(exterior)){%>
									<li>
										<span class="">外观: </span><span class="exterior"><%=exterior %></span>
									</li>
								<%} %>
								<li>
									<span class="">数量：</span><span class="quantity"><%=quantity %></span>
								</li>
								<li>
									<span class="label label-warning">分类:</span><span class="type"><a href="/bookcrossing/search?type=<%=type %>"><%=type %></a></span>
								</li>
								<li>
									<span class="label label-success">主人:</span><span class="belong"><a href="/bookcrossing/user/profile?uid=<%=belong %>" target="_blank"><%=belong %></a></span>
								</li>
								<li>
									<span class="label label-default">ISBN:</span><span class="isbn"><%=isbn %></span>
								</li>
							</ul>
						</div>
					</div>
					<div class="b-i-right">
						<div class="score-wrap">
							<p class="avg-score">
								<span class="bigstar <%=bigstarClass %>"></span><span class="score-num"><%=avg %></span>
							</p>
							<p>
								(<%=count %>)人评价
							</p>
							<p>
								<span class="smallstar sm-star-5" title="太好看啦"></span><span class="percent"><%=tentCount %>%</span>
							</p>
							<p>
								<span class="smallstar sm-star-4" title="挺不错的"></span><span class="percent"><%=eightCount %>%</span>
							</p>
							<p>
								<span class="smallstar sm-star-3" title="还过得去"></span><span class="percent"><%=sixCount %>%</span>
							</p>
							<p>
								<span class="smallstar sm-star-2" title="没啥感觉"></span><span class="percent"><%=foreCount %>%</span>
							</p>
							<p>
								<span class="smallstar sm-star-1" title="垃圾"></span><span class="percent"><%=twoCount %>%</span>
							</p>
						</div>
					</div>
					<div class="b-i-bottom">
						<div class="give-score-wrap">
							<span class="give-score-label">
								<%if(myScore!=0){ //是否已经评分%>
									我的评价：
								<%}else{ %>
									评价：
								<%} %>
							</span>
							<ul class="give-score">
								<li>
									<a href="javascript:void(0)" class="one" title="2"></a>
								</li>
								<li>
									<a href="javascript:void(0)" class="two" title="4"></a>
								</li>
								<li>
									<a href="javascript:void(0)" class="three" title="6"></a>
								</li>
								<li>
									<a href="javascript:void(0)" class="fore" title="8"></a>
								</li>
								<li>
									<a href="javascript:void(0)" class="five" title="10"></a>
								</li>
								<div class="score-selected" style="width:<%=(myScore/2*15) %>px"></div>
							</ul>
							<p class="score-tips"><%=tip %></p>
							<a href="javascript:void(0)" class="del-score" style="display:<%=delDisplay %>">删除评价</a>
						</div>
						<%if(belong.equals(uid)){%>
						<div class="del-book-btn-wrap">
							<a href="/bookcrossing/book/del?bid=" class="label label-warning del-book-btn">删除</a>
						</div>
						<%	if(ischeck!=2){ %>
						<div class="edit-book-btn-wrap">
							<a href="/bookcrossing/book/edit?bid=<%=bid %>" class="label label-primary edit-book-btn">编辑</a>
						</div>
						<%
							}
						}else if(isRole){ 
						%>
						<div class="del-book-btn-wrap">
							<a href="/bookcrossing/book/del?bid=" class="label label-warning del-book-btn">删除</a>
						</div>
						<%} %>
						<div class="debit-book-btn-wrap">
						<%if(!belong.equals(uid)){ 
							if(debit.equals(uid)){
						%>	
							<span class="label label-default">已借</span>			
						<%}else if(isSendDebit){
						%>
							<span class="label label-default">已申请借书</span>
						<%}else{
						%>
							<a href="javascript:void(0)" class="label label-info debit-book-btn">我要借</a>
						<%
							}
						}
						%>
						</div>
						<div class="b-i-introduce">
							<p class="b-i-i-title"><span class="label label-success">内容简介</span></p>
							<p class="b-i-i-content"><%=introduce %></p>
						</div>
						<div class="b-i-comment">
							<p class="b-i-c-title"><span class="label label-info">相关评论</span></p>
							<%
								int j = 1;
								for(int i=0; i<bcAll.size(); i++){
									BookComment cbookComment = (BookComment)bcAll.get(i);
									if(cbookComment.getBccontents()!=null){
										//取得用户对此书的评分
										List singleAll = null;
										int singleScore = 0 ;
										try{
											singleAll = Factory.getBookDAOInstance().queryBookCommentByBidAndUid(Long.toString(bid),cbookComment.getUid());	
										}catch(Exception ex){}
										if(singleAll.size()>0){
											BookComment singleBc = (BookComment)singleAll.get(0);
											singleScore = singleBc.getScore(); 
										}
							%>
								<div class="b-i-c-content">
									<div class="b-i-c-c-left">
										<img src="/bookcrossing/img/user/<%=cbookComment.getPic() %>" alt="头像"/>
									</div>
									<div class="b-i-c-c-right">
										<p class="b-i-c-c-r-top">
											<a href="/bookcrossing/user/profile?uid=<%=cbookComment.getUid() %>" target="_blank"><%=cbookComment.getUid() %></a>
											<span class="b-i-c-c-r-introduce">
												<%if(cbookComment.getIntroduce()!=null && !"".equals(cbookComment.getIntroduce())){%>
													(<%=cbookComment.getIntroduce() %>)
												<%}%>
											</span>
											<%if(singleScore!=0){%>
												<span class="smallstar sm-star-<%=singleScore/2 %>" title="<%=tips[singleScore/2-1] %>"></span>
											<%} %>
										</p>
										<p class="b-i-c-c-r-com">
											<%=cbookComment.getBccontents() %>
										</p>
										<p class="b-i-c-c-r-time">
											<%=cbookComment.getBctime() %>
										</p>
									</div>
									<%if(isRole || cbookComment.getUid().equals(uid) ){ %>
									<button type="button" class="close del-btn" data-bcid="<%=cbookComment.getBcid()%>" title="删除评论">&times;</button>
									<%} %>
									<span class="comment-floor">#<%=j %></span>
								</div>
							<%
										j++;
									}
								} 
							%>
						</div>
					</div>
					<div class="b-i-give-comment">
						<form id="comment-form" class="form" action="/bookcrossing/book/bookComment" method="post">
							<textarea name="comment" class="form-control" id="comment" rows="4" placeholder="骚年，滚下键盘吧~"></textarea>
							<button type="submit" class="btn btn-primary btn-sm comment-btn">评论</button>
						</form>
					</div>
				</div>
				<div class="clearfix"></div>
				<input type="hidden" value="<%=bid %>" class="hidden-bid"/>
				<input type="hidden" value="<%=bcid %>" class="hidden-bcid"/>
				<input type="hidden" value="<%=isRole %>" class="hidden-isrole"/>
			</div>
			<div class="toTop-wrap">
	       		<a href="javascript:;" id="toTop" title="返回顶部"></a>
	       	</div>
		</div>
		<jsp:include page="../public/foot.jsp"></jsp:include>
		<script src="/bookcrossing/js/jquery-1.9.1.min.js" type="text/javascript"></script>
        <script src="/bookcrossing/js/bootstrap.min.js" type="text/javascript" ></script>
        <script src="/bookcrossing/js/common.js" type="text/javascript" ></script>
        <script src="/bookcrossing/js/book-info.js" type="text/javascript" ></script>
	</body>